import request from '@/utils/request'

// =============================================
// 配送员端API接口
// 基于数据库视图实现配送员端功能
// =============================================

// 1. 配送员认证相关
export function deliverymanLogin(data) {
    return request({
        url: '/DeliveryApi/deliveryman/login',
        method: 'post',
        data
    })
}

export function deliverymanLogout() {
    return request({
        url: '/DeliveryApi/deliveryman/logout',
        method: 'post'
    })
}

export function getDeliverymanProfile() {
    return request({
        url: '/DeliveryApi/deliveryman/profile',
        method: 'get'
    })
}

export function updateDeliverymanProfile(data) {
    return request({
        url: '/DeliveryApi/deliveryman/profile',
        method: 'put',
        data
    })
}

// 2. 任务包管理
export function getTaskPackages(params) {
    return request({
        url: '/DeliveryApi/taskPackages',
        method: 'get',
        params
    })
}

export function getTaskPackageDetail(packageId) {
    return request({
        url: `/DeliveryApi/taskPackages/${packageId}`,
        method: 'get'
    })
}

export function acceptTaskPackage(packageId) {
    return request({
        url: `/DeliveryApi/taskPackages/${packageId}/accept`,
        method: 'post'
    })
}

export function startTaskPackageDelivery(packageId) {
    return request({
        url: `/DeliveryApi/taskPackages/${packageId}/start`,
        method: 'post'
    })
}

export function completeTaskPackage(packageId) {
    return request({
        url: `/DeliveryApi/taskPackages/${packageId}/complete`,
        method: 'post'
    })
}

// 3. 订单操作
export function getOrdersByPackage(packageId) {
    return request({
        url: `/DeliveryApi/taskPackages/${packageId}/orders`,
        method: 'get'
    })
}

export function scanPickupOrder(orderNumber, barcode) {
    return request({
        url: '/DeliveryApi/orders/scanPickup',
        method: 'post',
        data: { orderNumber, barcode }
    })
}

export function signOrder(orderNumber, signature) {
    return request({
        url: '/DeliveryApi/orders/sign',
        method: 'post',
        data: { orderNumber, signature }
    })
}

export function reportOrderException(orderNumber, exceptionType, description) {
    return request({
        url: '/DeliveryApi/orders/exception',
        method: 'post',
        data: { orderNumber, exceptionType, description }
    })
}

// 4. 配送员状态管理
export function updateDeliverymanStatus(status) {
    return request({
        url: '/DeliveryApi/deliveryman/status',
        method: 'put',
        data: { status }
    })
}

export function updateDeliverymanLocation(latitude, longitude) {
    return request({
        url: '/DeliveryApi/deliveryman/location',
        method: 'put',
        data: { latitude, longitude }
    })
}

// 5. 配送员绩效统计
export function getDeliverymanPerformance(params) {
    return request({
        url: '/DeliveryApi/deliveryman/performance',
        method: 'get',
        params
    })
}

export function getDeliverymanStats() {
    return request({
        url: '/DeliveryApi/deliveryman/stats',
        method: 'get'
    })
}

// 6. 路线规划
export function getDeliveryRoute(packageId) {
    return request({
        url: `/DeliveryApi/routes/${packageId}`,
        method: 'get'
    })
}

export function optimizeDeliveryRoute(packageId) {
    return request({
        url: `/DeliveryApi/routes/${packageId}/optimize`,
        method: 'post'
    })
}

// 7. 消息通知
export function getDeliverymanNotifications() {
    return request({
        url: '/DeliveryApi/deliveryman/notifications',
        method: 'get'
    })
}

export function markNotificationRead(notificationId) {
    return request({
        url: `/DeliveryApi/deliveryman/notifications/${notificationId}/read`,
        method: 'put'
    })
}

// 8. 扫码功能
export function scanBarcode(barcode) {
    return request({
        url: '/DeliveryApi/scan/barcode',
        method: 'post',
        data: { barcode }
    })
}

export function scanQRCode(qrCode) {
    return request({
        url: '/DeliveryApi/scan/qrcode',
        method: 'post',
        data: { qrCode }
    })
}

// 9. 异常处理
export function getExceptionTypes() {
    return request({
        url: '/DeliveryApi/exceptions/types',
        method: 'get'
    })
}

export function submitExceptionReport(data) {
    return request({
        url: '/DeliveryApi/exceptions/report',
        method: 'post',
        data
    })
}

// 10. 工作记录
export function getWorkHistory(params) {
    return request({
        url: '/DeliveryApi/deliveryman/workHistory',
        method: 'get',
        params
    })
}

export function getTodayWorkSummary() {
    return request({
        url: '/DeliveryApi/deliveryman/todaySummary',
        method: 'get'
    })
}

// 11. 客户联系
export function getCustomerContact(orderNumber) {
    return request({
        url: `/DeliveryApi/orders/${orderNumber}/customer`,
        method: 'get'
    })
}

export function callCustomer(orderNumber) {
    return request({
        url: `/DeliveryApi/orders/${orderNumber}/call`,
        method: 'post'
    })
}

// 12. 配送员设置
export function getDeliverymanSettings() {
    return request({
        url: '/DeliveryApi/deliveryman/settings',
        method: 'get'
    })
}

export function updateDeliverymanSettings(data) {
    return request({
        url: '/DeliveryApi/deliveryman/settings',
        method: 'put',
        data
    })
}

// 13. 实时数据
export function getRealtimeOrders() {
    return request({
        url: '/DeliveryApi/realtime/orders',
        method: 'get'
    })
}

export function getRealtimeStats() {
    return request({
        url: '/DeliveryApi/realtime/stats',
        method: 'get'
    })
}

// 14. 文件上传
export function uploadSignature(orderNumber, file) {
    const formData = new FormData()
    formData.append('signature', file)
    formData.append('orderNumber', orderNumber)
    
    return request({
        url: '/DeliveryApi/orders/uploadSignature',
        method: 'post',
        data: formData,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

export function uploadExceptionPhoto(orderNumber, file) {
    const formData = new FormData()
    formData.append('photo', file)
    formData.append('orderNumber', orderNumber)
    
    return request({
        url: '/DeliveryApi/exceptions/uploadPhoto',
        method: 'post',
        data: formData,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 15. 数据同步
export function syncDeliveryData() {
    return request({
        url: '/DeliveryApi/sync/data',
        method: 'post'
    })
}

export function getSyncStatus() {
    return request({
        url: '/DeliveryApi/sync/status',
        method: 'get'
    })
} 